package com.xiaoyu.fish.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@Component
public class LoginInterceptor implements HandlerInterceptor {

    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
    private static final ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 1.判断是否是静态资源，是否需要放行
     * 2.判断会否登录
     */
    String[] urls = new String[]{
            "/map.html",
            "/welcome.html"
    };

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //请求路径
        //访问静态资源
        String requestURI = request.getRequestURI();
        for (String url : urls) {
            boolean match = antPathMatcher.match(url, requestURI);
            if (match){
                return true;
            }
        }
        //如果访问的不是静态资源，则需要查看是否登录


        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
